<?php

namespace app\admin\controller;

use app\common\controller\BasicAdmin;
use app\common\model\UserModel;
use app\common\traits\SelectPage;

class CourseAttendance extends BasicAdmin
{
    use SelectPage;

    protected $model = 'CourseAttendanceModel';

    public function index(){
        $this->title = '学生考勤管理';

        if(input('?attendance_id') && input('attendance_id')){
            $course = model('CourseAttendanceModel')
                ->join('edu_course c','c.id=edu_course_attendance.course_id','INNER')
                ->where('edu_course_attendance.id',input('attendance_id'))
                ->field('edu_course_attendance.*,c.name')
                ->find();
            $this->title = $course['name'];
            $this->assign('course',explode(',',$course['in_userids']));
            return $this->_query(model('UserModel'))->where(function ($db) use ($course){
                $db->where('id','in',$course['all_userids']);
            })->page();
        }

        if(input('?course_id') && !empty(input('course_id'))){
            $course = model('CourseAttendanceModel')->where('course_id',input('course_id'))->find();
            $this->assign('course',explode(',',$course['in_userids']));
            return $this->_query(model('UserModel'))->where(function ($db) use ($course){
                $db->where('id','in',$course['all_userids']);
            })->page();
        }else{
            $user = model('UserModel')
                ->where(function ($db){
                    if($this->request->has('school_id','get') && !empty($this->request->get('school_id'))){
                        $status = $this->request->get('school_id');
                        $db->where('school_id', $status);
                    }
                    if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
                        $cate_id = $this->request->get('class_id');
                        $db->where('class_id', $cate_id);
                    }
                    if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
                        $school_id = $this->request->get('grade_id');
                        $db->where('grade_id', $school_id);
                    }
                })
                ->where('role','>',1)
                ->field('id')
//                ->fetchSql()
                ->select()
                ->toArray()
            ;

            if($user){
                $user = array_column($user,'id');
                $user = implode(',',$user);
            }
            $course = model('CourseAttendanceModel')
                ->where('teacher_id','in',$user)
                ->order('id DESC')
                ->find();
            $this->assign('course',explode(',',$course['in_userids']));
            return $this->_query(model('UserModel'))->where(function ($db) use ($course){
                $db->where('id','in',$course['all_userids']);
            })->page();

        }

    }

    public function attendanceList(){
        $c =  $this->_query($this->model)
            ->join('edu_user u','u.id=edu_course_attendance.teacher_id','INNER')
            ->join('edu_course c','c.id=edu_course_attendance.course_id','INNER')
            ->where(function ($db){
                if($this->request->has('school_id','get') && !empty($this->request->get('school_id'))){
                    $status = $this->request->get('school_id');
                    $db->where('u.school_id', $status);
                }
                if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
                    $cate_id = $this->request->get('class_id');
                    $db->where('u.class_id', $cate_id);
                }
                if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
                    $school_id = $this->request->get('grade_id');
                    $db->where('u.grade_id', $school_id);
                }
                if($this->request->has('course_id','get') && !empty($this->request->get('course_id'))){
                    $school_id = $this->request->get('course_id');
                    $db->where('edu_course_attendance.course_id', $school_id);
                }
            })
            ->field('u.realname,edu_course_attendance.*,c.name AS course_name')
//            ->fetchSql()
            ->page();

        return ($c);
    }

    public function teacher(){


        $c =  $this->_query(model('UserSignInModel'))
            ->join('edu_school school','school.id=edu_user_sign_in.school_id','left')
            ->join('edu_school_class class','class.id=edu_user_sign_in.class_id','left')
            ->join('edu_school_grade grade','grade.id=edu_user_sign_in.grade_id','left')
            ->join('edu_course c','c.id=edu_user_sign_in.course_id','left')
            ->where(function ($db){
                if($this->request->has('school_id','get') && !empty($this->request->get('school_id'))){
                    $status = $this->request->get('school_id');
                    $db->where('edu_user_sign_in.school_id', $status);
                }
                if($this->request->has('class_id','get') && !empty($this->request->get('class_id'))){
                    $class_id = $this->request->get('class_id');
                    $db->where('edu_user_sign_in.class_id', $class_id);
                }
                if($this->request->has('grade_id','get') && !empty($this->request->get('grade_id'))){
                    $school_id = $this->request->get('grade_id');
                    $db->where('edu_user_sign_in.grade_id', $school_id);
                }
                if($this->request->has('course_id','get') && !empty($this->request->get('course_id'))){
                    $course_id = $this->request->get('course_id');
                    $db->where('edu_user_sign_in.course_id', $course_id);
                }
            })
            ->field('
                    edu_user_sign_in.*,
                    school.name school_name,
                    class.name class_name,
                    grade.name grade_name,
                    c.name course_name
                ')

            ->page();

        dump($c);
    }



}